package com.tj.jinzao.widgets;


import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;

import com.tj.jinzao.R;
import com.tj.jinzao.adapter.ChooseFamilyMemberDialogAdapter;
import com.tj.jinzao.model.FamilyMember;
import com.tj.jinzao.utils.ToastUtils;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 选择人物
 * Created by wjy on 2019/1/6.s
 */
public class ChoosePeopleDialog extends Dialog implements TextWatcher {

    public ChoosePeopleDialog(Context context) {
        super(context);
    }

    public ChoosePeopleDialog(Context context, int theme) {
        super(context, theme);
    }

    private String mContent;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        mContent = s.toString().trim();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public static class Builder implements View.OnClickListener,TextWatcher{
        private Context mContext;
        private View.OnClickListener mOnClickListener;
        private EditText mEditText;
        private View mLayout;
        private View mChooseLayout, mAddLayout;
        private String mName;
        private RecyclerView mRecyclerView;

        private CopyOnWriteArrayList<FamilyMember> mList;

        public Builder setFamilyList(List<FamilyMember> list) {
            mList = new CopyOnWriteArrayList();
            if(list != null)mList.addAll(list);
            return this;
        }

        private ChoosePeopleDialog mDialog;

        public Builder(Context context) {
            this.mContext = context;
        }

        public Builder setOnClickListener(View.OnClickListener listener) {
            this.mOnClickListener = listener;
            return this;
        }

        public ChoosePeopleDialog create() {
            return this.create(R.layout.dialog_choose_people);
        }

        public ChoosePeopleDialog create(int layoutRes) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            mDialog = new ChoosePeopleDialog(mContext, R.style.Dialog);
            mLayout = inflater.inflate(layoutRes, null);
            mChooseLayout = mLayout.findViewById(R.id.ll_choose_layout);
            mAddLayout = mLayout.findViewById(R.id.rl_add_layout);
            mLayout.findViewById(R.id.btn_self).setOnClickListener(this);
            mLayout.findViewById(R.id.btn_other).setOnClickListener(this);
            mLayout.findViewById(R.id.btn_confirm).setOnClickListener(this);
            mLayout.findViewById(R.id.iv_cancel).setOnClickListener(this);
            mEditText = mLayout.findViewById(R.id.et_name);
            mRecyclerView = mLayout.findViewById(R.id.recycler_view);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
            ChooseFamilyMemberDialogAdapter adapter = new ChooseFamilyMemberDialogAdapter(mContext,mList);
            adapter.setListener(mOnClickListener);
            mRecyclerView.setAdapter(adapter);
            mEditText.addTextChangedListener(this);
            mDialog.addContentView(mLayout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            return mDialog;
        }

        public View getLayout() {
            return mLayout;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_other:
                    mAddLayout.setVisibility(View.VISIBLE);
                    mChooseLayout.setVisibility(View.GONE);
                    break;
                case R.id.btn_confirm:
                    if(!TextUtils.isEmpty(mName)){
                        v.setTag(mName);
                        if (mOnClickListener != null) {
                            mOnClickListener.onClick(v);
                        }
                    }else {
                        ToastUtils.showToast(mContext,"请输入名字");
                    }
                    break;
                case R.id.btn_self:
                    if (mOnClickListener != null) {
                        mOnClickListener.onClick(v);
                    }
                    break;
                case R.id.iv_cancel:
                    mDialog.dismiss();
                    break;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mName = s.toString();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    public String getContent() {
        return mContent;
    }
}
